<?php
namespace app\index\controller;
use think\Request;
use think\Db;
use cmf\controller\HomeBaseController;

class WeixinController extends HomeBaseController
{
    public function index()
    {
      $conf=cmf_get_option("site_info");
      $data=Request::instance()->get();
      $appid="wx31feb6c472dcd373";
      $key="1f4ccd50dffbb6438a939f08364f597c";
            if(!isset($data['id'])){
                //判断是否有上级id
                //
                 if(isset($data['qrcode'])){
                      //加载fastpay支付插件
                      //session("tuid",$qrcode);
                      $myurl=$conf['gameurl']."/index/weixin/open";
                      $wxurl='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$myurl.'&response_type=code&scope=snsapi_userinfo&state='.$data['qrcode'].'#wechat_redirect';
                      header('Location:'.$wxurl);
            exit();
                }else{
           $myurl=$conf['gameurl']."/index/weixin/open";
                      $wxurl='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$myurl.'&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect';
                      header('Location:'.$wxurl);
            exit();
                }

         }
    }
    public function open()
    {
          $request = Request::instance();
          $appid="wx31feb6c472dcd373";
          $key="1f4ccd50dffbb6438a939f08364f597c";
          $code   = $_GET['code'];
          $conf=cmf_get_option("site_info");
          if ($code) {
              //初始化
              $ch = curl_init();
              //设置选项，包括URL
              // curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$key&code=$code&grant_type=authorization_code");
              curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/sns/oauth2/access_token?grant_type=authorization_code&appid=$appid&secret=$key&code=$code");
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_HEADER, 0);
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
              curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
              //执行并获取HTML文档内容
              $output = curl_exec($ch);
              //释放curl句柄
              curl_close($ch);
          }

          $oauth2 = json_decode($output,true);
      
      
           if(empty($oauth2["access_token"])){
                      $urls = $conf['gameurl']."/index/weixin/index?qrcode=".$_GET['state'];
                      echo "<script language='javascript' type='text/javascript'>"; 
                      echo "window.location.href='$urls'"; 
                      echo "</script>"; 
            }
      
      
            $access_token = $oauth2["access_token"];
            //openid
            $openid = $oauth2['openid'];

            //第二步:根据全局access_token和openid查询用户信息
            $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
            $userinfo = $this->http_curl($get_user_info_url);
          //print_r($userinfo);
          //存入上级和用户信息token
          if($userinfo){
                    $user=Db::name('user')->where('open',$userinfo['openid'])->find();
                    if($user){
                          if($user['user_status']==1){
                            Db::name('user')->where('id', $user['id'])->update(['last_login_time' => time(),'last_login_ip'=>$request->ip()]);
              session("userid",$user['id']);
                            if(!$user['txopen']){
                                  $urls = $conf['gameurl']."/index/api/jfopen.html";
                                    $url = "http://api.0948.xyz//api/api/getOpenid?redirect_uri=".$urls."?u=1"; 
                                    echo "<script language='javascript' type='text/javascript'>"; 
                                    echo "window.location.href='$url'"; 
                                    echo "</script>"; 
                                  exit;
                            }
                            
                          $this->redirect('index/index');
                          }else{
                            $geturl="http://aqiyi.com";
                            exit("<meta http-equiv='Refresh' content='0;URL={$geturl}'>");
                          }

                    }else{
                        $tle=rand(111,222);
                        $arr['open']=$userinfo['openid'];
                        $arr['user_type']=2;
                        $arr['last_login_time']=time();
                        $arr['score']=0;
                        $arr['coin']=0;
                        $arr['balance']=0;
                        $arr['create_time']=time();
                        
                        $nick_login=nicknum();
                        
                        $arr['user_login']=$nick_login;
                        $arr['user_pass']=md5(123);
                        $arr['token']=cmf_password($userinfo['openid'].$tle);
                        $arr['token_time']=time()+5000;
                        $arr['user_nickname']=$userinfo['nickname'];
                        $arr['user_email']="123@123.com";
                        $arr['avatar']=$userinfo['headimgurl'];
                        $arr['last_login_ip']=$request->ip();
                        $arr['mobile']=$tle;
                        if(isset($_GET['state'])){ 
                            $uu=Db::name('user')->where(['id'=>$_GET['state']])->find();
                            $arr['ss0']=$_GET['state']?:0;
                            $arr['ss1']=$uu['ss0']?:0;
                            $arr['ss2']=$uu['ss1']?:0;
                            $arr['ss3']=$uu['ss2']?:0;
                            $arr['ss4']=$uu['ss3']?:0;
                            $arr['ss5']=$uu['ss4']?:0;
                            $arr['ss6']=$uu['ss5']?:0;
                        }
                        $user=Db::name('user')->insert($arr);
                        //$newarr['user']=
                      
                        //$lodings=Db::name('newloding')->insert($newarr);
                      
                        $this->redirect('index/index');
                        //cookie("usertoken",$user['token'], 2000);
                        session("userid",$user['id']);
                        //$this->redirect('/vue/index.html',302);
                    }
                   
            }
      
          
    }
    public function http_curl($url){
      //用curl传参
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

      //关闭ssl验证
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);


      curl_setopt($ch,CURLOPT_HEADER, 0);
      $output = curl_exec($ch);
      curl_close($ch);
      return json_decode($output, true);
  }
}
